<?php
session_start();

//require_once("includes/connect.php");
//require_once("includes/function.php");

$panngan2 = $_REQUEST["panngan2_id"];
$panngan3 = $_REQUEST["panngan3_id"];

$submit = $_REQUEST["submit"];

$budget_year = $_REQUEST["budget_year"];

$month_year = $_REQUEST["month_year"];

//$ex1_id = $_REQUEST["ex1_id"];
$ex1_id = 2;
$ex2_id = 3;
$ex3_id = $_REQUEST["ex3_id"];
?>

<script type="text/javascript">
    $(document).ready(function() {

        $.post('dropdown.php', {tablename: 'expenses_lvl1', dd: 'ex1_id', idname: 'expenses1_id', fieldname: 'expenses1_name', by: '2', ex1_id: '<?php echo $ex1_id; ?>', ex2_id: '<?php echo $ex2_id; ?>', ex3_id: '<?php echo $ex3_id; ?>'}, function(data) {
            $("#expenses1").html(data);
        });

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                //event.preventDefault();
            }
        });

        $("#panngan2_id").change(function() {
            var panngan2 = $(this).val();
            var panngan3 = "<?php echo $panngan3; ?>";

            //alert(panngan2);

            if (panngan2 != "") {
                //alert(panngan2);
                $.post('dd_panngan_1.php', {panngan2: panngan2, panngan3: panngan3}, function(data) {
                    $("#dd_panngan3").html(data);

                });
            }

        });

        $("#month_year").datepicker({
            dateFormat: "yy-mm"
                    //defaultDate: new Date()
                    //showMonthAfterYear:true,

        });

        $('#panngan2_id').trigger('change');

        $("#save_salary").click(function() {
            var val = [];
            var count = 0;
            $(':checkbox:checked').each(function(i) {
                val[i] = $(this).val();
                count++;
                //alert(val[i]);
            });
            //alert(count);
            if (count == 0) {
                alert("กรุณาเลือกข้อมูลที่ต้องบันทึกด้วยครับ");
            } else {

                var panngan2 = $("#panngan2_id").val();
                var panngan3 = $("#panngan3_id").val();
                
                var date_create = $("#date_create").val();
                var comment = $("#comment").val();

                $.post('process_petition_welfare.php', {
                    panngan2: '<?php echo $panngan2; ?>'
                    , panngan3: '<?php echo $panngan3; ?>'
                    , date_create: date_create
                    , comment: comment
                    , month_year: '<?php echo $month_year; ?>'
                    , budget_year: '<?php echo $budget_year; ?>'
                    , ex1_id: '<?php echo $ex1_id; ?>'
                    , ex2_id: '<?php echo $ex2_id; ?>'
                    , ex3_id: '<?php echo $ex3_id; ?>'
                    , val: val
                    , task: 'new'}, function(data) {
                    //$("#test").html(data); 

                    //if(confirm('คุณต้องการพิมพ์ฎีกาเงินเดือนหรือไม่?')==true){
                    //alert('Going to http://www.thaicreate.com');
                    //window.location.href = 'prt_welfare?pet_id=' + data;
                    //window.open("prt_salary?pet_id=" + data, "_blank");
                    //window.location.href = "index.php?pages=frm_petition_salary&pageid=1";
                    //alert("print");
                    //} else 	{
                    window.location.href = "index.php?pages=frm_petition_welfare&pageid=2";
                    //}


                    //alert(data);

                });

            }
        });

    }); //(document).ready


</script>
<div id="test"></div>
<form id="frm_petition_welfare_add" name="frm_petition_welfare_add" method="post" action="index.php" novalidate="">
    <input type="hidden" name="pages" value="frm_petition_welfare_add">
    <input type="hidden" name="pageid" value="2">
    <div class="row-fluid">
        <div class="control-group span2">
            <label class="control-label" for="budget_year">ปีงบประมาณ</label>
            <div class="controls ">
                <select class="span12" id="budget_year" name="budget_year" >
                    <!--                    <option>2556</option>
                                        <option>2557</option>
                                        <option>2558</option>
                                        <option>2559</option>
                                        <option>2560</option>-->
                    <?php
                    $this_y = date("Y");

                    if ($this_y < 2556)
                        $this_y += 543;

                    if (!$budget_year)
                        $budget_year = $this_y;

                    for ($i = 2556; $i <= $this_y + 1; $i++) {
                        if ($i == $budget_year) {
                            $selected = "selected";
                        } else {
                            $selected = "";
                        }
                        echo "<option value='$i' $selected>" . $i . "</option>";
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="control-group span4">
            <label class="control-label" for="panngan">แผนงาน</label>
            <div class="controls ">
                <select class="span12" id="panngan2_id" name="panngan2_id" required>
                    <option value="">เลือก...</option>
                    <?php
                    $db = NEW myDBS;
                    $db->selectDB("hr_udoncity");

                    $sql = "SELECT DISTINCT panngan2_id 
                            FROM position 
                            WHERE lvl_id like '" . $_SESSION["lvl1_id"] . "%'
                            AND panngan2_id IS NOT NULL"
                    ;

                    //echo $sql;

                    $result = $db->runSQL($sql);

                    while ($rows = $db->fetch_object($result)) {
                        $db->selectDB("account");
                        $sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" . $rows->panngan2_id . "'";
                        $result1 = $db->runSQL($sql);
                        while ($rows1 = $db->fetch_object($result1)) {
                            if ($rows1->panngan2_id == $panngan2) {
                                $selected = " selected";
                            } else {
                                $selected = "";
                            }
                            echo "<option value='" . $rows1->panngan2_id . "' $selected>$rows1->panngan2_name</option>";
                        }
                    }
                    ?>
                </select>
            </div>
        </div><!--/span-->

        <div class="control-group span4">
            <label class="control-label" for="panngan3">งาน</label>
            <div class="controls " id="dd_panngan3">
                <select class="span12" id="panngan3_id" name="panngan3_id">
                    <option value="">เลือก</option>

                </select>
            </div>
        </div><!--/span-->

        <div class="control-group span2">
            <label class="control-label" for="month_year">เดือน/ปี</label>
            <div class="controls">
                <input class="span12" type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" required />
            </div>
        </div>
    </div><!--/row-fluid-->

    <div class="row-fluid">
        <div class="control-group span4">
            <label class="control-label" for="expenses1_name">หมวด / ลักษณะ</label>
            <div id="expenses1" class="controls">
                <select class="span12" id="ex1_id" name="ex1_id" required>
                    <option value="">เลือก...</option>
                </select>
            </div>
        </div>

        <div class="control-group span3">
            <label class="control-label" for="expenses2_name">ประเภท</label>
            <div id="expenses2" class="controls">
                <select class="span12" id="ex2_id" name="ex2_id" required>
                    <option value="">เลือก...</option>
                </select>
            </div>
        </div>

        <div class="control-group span3">
            <label class="control-label" for="expenses3_name">รายการ</label>
            <div id="expenses3" class="controls">
                <select class="span12" id="ex3_id" name="ex3_id" required>
                    <option value="">เลือก...</option>
                </select>
            </div>
        </div>

        <div class="control-group span2">
            <label class="control-label" for="blank">&nbsp;</label>
            <button type="submit" id="submit" name="submit"  class="btn btn-info"><i class="icon-ok icon-white"></i>ดึงข้อมูล</button>
            <!--<a href="index.php?pages=home" role="button" class="btn"><i class="icon-remove"></i>ยกเลิก</a>-->
        </div>
    </div><!--/row-fluid-->

</form> 
<?php
if (isset($submit)) {
    //echo "onsubmit";
    $sql = "SELECT * 
            FROM  tbl_init 
            WHERE  panngan2_id =  '$panngan2'
            AND  panngan3_id =  '$panngan3'
            AND  month_year =  '$month_year'
            AND  budget_year =  '$budget_year'
            ";

    switch ($ex3_id) {
        case 48:
            $sql = $sql . "AND rent_home > 0";
            break;
        case 49:
            $sql = $sql . "AND education > 0";
            break;
        case 50:
            $sql = $sql . "AND medical_fee > 0";
            break;
    }
    //echo $sql;
    $db->selectDB("account");
    $results = $db->runSQL($sql);
    ?>

    <table id="example" class="table table-bordered" width="100%" >
        <caption><br></caption>
        <thead>
            <tr>
                <th>#</th>
                <th>เลขประจำตัว</th>
                <th>ชื่อ - นามสกุล</th>
                <th>ตำแหน่ง</th>
                <th>
                    <?php
                    switch ($ex3_id) {
                        case 48:
                            echo "ค่าเช่าบ้าน";
                            break;
                        case 49:
                            echo "เงินช่วยเหลือการศึกษาบุตร";
                            break;
                        case 50:
                            echo "เงินช่วยเหลือค่ารักษาพยาบาล";
                            break;
                    }
                    ?>
                </th>
                <th>action</th>
                <th>สถานะ</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = 0;
            $db1 = NEW myDBS;
            $db1->selectDB("hr_udoncity");

            while ($rows = $db->fetch_object($results)) {
                $sql = "SELECT fname, lname, position_name FROM person, position
					WHERE position.id = person.id
					AND person.id = '" . $rows->card_id . "'
					";
                $results1 = $db1->runSQL($sql);
                //echo $sql;
                $rows1 = $db1->fetch_object($results1);
                $i++;
                echo "<tr>
                <td>$i</td>
                <td>$rows->card_id</td>
                <td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
                <td>$rows1->position_name</td>
                <td ><p class='text-right'>
                ";

                switch ($ex3_id) {
                    case 48:
                        echo number_format($rows->rent_home, 2);
                        break;
                    case 49:
                        echo number_format($rows->education, 2);
                        break;
                    case 50:
                        echo number_format($rows->medical_fee, 2);
                        break;
                }
                echo "</p></td>";
                
                switch ($ex3_id) {
                    case 48:
                        if (strpos($rows->status, 'rent_home') == false) {
                            echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id' /></td>  ";
                        } else {
                            echo "<td></td>";
                        }
                        break;
                    case 49:
                        if (strpos($rows->status, 'education') == false) {
                            echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id' /></td>  ";
                        } else {
                            echo "<td></td>";
                        }
                        break;
                    case 50:
                        if (strpos($rows->status, 'medical_fee') == false) {
                            echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id' /></td>  ";
                        } else {
                            echo "<td></td>";
                        }
                        break;
                }
                
//                if (strpos($rows->status, 'rent_home') == false || strpos($rows->status, 'education') == false || strpos($rows->status, 'medical_fee') == false) {
//                    //echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id' /></td>  ";
//                    echo "<td></td>";
//                } else {
//                    echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id' /></td>  ";
//                    //echo "<td></td>";
//                }
//                
                echo "<td>";
                switch ($ex3_id) {
                    case 48:
                        if (strpos($rows->status, 'rent_home') <> false) {
                            echo "เบิกค่าเช่าบ้านแล้ว";
                        }
                        break;
                    case 49:
                        if (strpos($rows->status, 'education') <> false) {
                            echo "เบิกค่าศึกษาบุตรแล้ว";
                        }
                        break;
                    case 50:
                        if (strpos($rows->status, 'medical_fee') <> false) {
                            echo "เบิกค่ารักษาพยาบาลแล้ว";
                        }
                        break;
                }
                echo "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
    <div class="control-group">
        <div class="control-group">
            <label class="control-label" for="วันที่">วันที่</label>
            <div class="controls">
                <input type="text" id="date_create" name="date_create" value="<?php echo date("Y-m-d"); ?>" required=""/>
            </div>
        </div>
        <label class="control-label" for="หมายเหตุ">หมายเหตุ</label>
        <div class="controls">
            <textarea id="comment" name="comment" rows="7" style="width: 350px" placeholder="เบิกจ่ายให้แก่... เพื่อ....."></textarea>

                <!--<input type="text" id="comment" name="comment" value="<?php echo $row->comment; ?>" />-->
        </div>
    </div>
    <button id="save_salary" name = "save_salary" class="btn btn-primary">บันทึกฏีกาสวัสดิการ</button>
    <?php
}
?>
